<template>
  <view class="template-detail-container">
    <nav-header title="模板详情"></nav-header>
    
    <view class="content" :style="{ paddingTop: statusBarHeight + 44 + 'px' }">
      <!-- Preview -->
      <view class="preview-container">
        <image class="preview-image" :src="template?.thumbnail" mode="widthFix"></image>
      </view>
      
      <!-- Template Info -->
      <view class="template-info">
        <view class="title">{{ template?.name }}</view>
        <view class="meta">
          <text>适用年限：{{ template?.workYears }}</text>
          <text class="divider">|</text>
          <text>类型：{{ getTypeLabel(template?.type) }}</text>
        </view>
      </view>
      
      <!-- Action Buttons -->
      <view class="action-buttons">
        <view class="action-btn favorite" @click="toggleFavorite">
          <u-icon :name="isFavorite ? 'star-fill' : 'star'" :color="isFavorite ? '#FFD700' : '#666'" size="22"></u-icon>
          <text>{{ isFavorite ? '已收藏' : '收藏' }}</text>
        </view>
        <view class="action-btn import" @click="importResume">
          <u-icon name="download" color="#666" size="22"></u-icon>
          <text>导入</text>
        </view>
        <view class="action-btn complete" @click="navigateToEditor">
          <u-icon name="edit-pen" color="#5ac725" size="22"></u-icon>
          <text>完善</text>
        </view>
      </view>
      
      <!-- Similar Templates -->
      <view class="similar-templates">
        <view class="section-title">相似推荐：</view>
        <view class="template-grid">
          <view 
            class="template-item" 
            v-for="(similar, index) in similarTemplates" 
            :key="index"
            @click="navigateToDetail(similar.id)"
          >
            <image :src="similar.thumbnail" mode="aspectFill"></image>
            <view class="info">
              <view class="name">{{ similar.name }}</view>
              <view class="year">工作年限: {{ similar.workYears }}</view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { getToken } from '@/utils/auth'
import { onLoad } from '@dcloudio/uni-app'
import { useTemplateStore } from '@/store/template.js'
import { useResumeStore } from '@/store/resume.js'
import NavHeader from '@/components/nav-header.vue'
import { useAuthStore } from '@/store/index';

const authStore = useAuthStore();
const templateStore = useTemplateStore()
const resumeStore = useResumeStore()
const statusBarHeight = ref(20)
const templateId = ref('')

// 当前选中的模板
const template = computed(() => {
  if (!templateId.value) return null
  return templateStore.getTemplateById(templateId.value)
})

// 类似模板推荐
const similarTemplates = computed(() => {
  if (!templateId.value) return []
  return templateStore.getSimilarTemplates(templateId.value)
})

// 是否已收藏
const isFavorite = computed(() => {
  if (!templateId.value) return false
  return templateStore.favoriteTemplates.includes(templateId.value)
})

// 获取模板类型标签
const getTypeLabel = (type) => {
  if (!type) return ''
  const found = templateStore.templateTypes.find(item => item.id === type)
  return found ? found.name : type
}

onLoad((options) => {
  if (options.id) {
    templateId.value = options.id
    templateStore.setCurrentTemplateId(options.id)
  } else if (templateStore.currentTemplateId) {
    templateId.value = templateStore.currentTemplateId
  }
})

onMounted(() => {
  // 获取状态栏高度
  const systemInfo = uni.getSystemInfoSync()
  statusBarHeight.value = systemInfo.statusBarHeight
  
  // 加载模板和收藏数据
  templateStore.loadFromStorage()
})

// 切换收藏状态
const toggleFavorite = () => {
  if(!getToken()){
    uni.showToast({
      title: '请先登录',
      icon: 'none'
    })
    return
  }
  if (!templateId.value) return
  
  if (isFavorite.value) {
    templateStore.removeFromFavorite(templateId.value)
    uni.showToast({
      title: '已取消收藏',
      icon: 'none'
    })
  } else {
    templateStore.addToFavorite(templateId.value)
    uni.showToast({
      title: '收藏成功',
      icon: 'none'
    })
  }
}

// 导入简历
const importResume = () => {
  uni.showToast({
    title: '导入功能开发中',
    icon: 'none'
  })
}

// 导航到简历编辑器
const navigateToEditor = () => {
  if(!getToken()){
    uni.showToast({
      title: '请先登录',
      icon: 'none'
    })
    return
  }
	console.log(authStore.userId)
	if(authStore.userId != '') {
		if (!templateId.value || !template.value) return
		// 设置模板信息
		resumeStore.setTemplate({
			id: templateId.value,
			name: template.value.name,
			type: template.value.type
		})
		// 添加到已使用模板
		templateStore.addToUsedTemplates(templateId.value)
			// 跳转到简历编辑页面
			uni.navigateTo({
			url: '/pages/resume/edit/index'
		})
	}else{
		authStore.getInfoData().then(()=> {
			if (!templateId.value || !template.value) return
			// 设置模板信息
			resumeStore.setTemplate({
				id: templateId.value,
				name: template.value.name,
				type: template.value.type
			})
			// 添加到已使用模板
			templateStore.addToUsedTemplates(templateId.value)
				// 跳转到简历编辑页面
				uni.navigateTo({
				url: '/pages/resume/edit/index'
			})
		})
	}
}

// 导航到其他模板详情
const navigateToDetail = (id) => {
  // 先更新当前ID
  templateId.value = id
  templateStore.setCurrentTemplateId(id)
  
  // 然后重载页面
  uni.redirectTo({
    url: `/pages/template/detail?id=${id}`
  })
}
</script>

<style lang="scss" scoped>
.template-detail-container {
  min-height: 100vh;
  background-color: #F8F8F8;
  
  .content {
    padding: 16px;
    
    .preview-container {
      background-color: #FFFFFF;
      border-radius: 12px;
      padding: 16px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
      margin-bottom: 16px;
      
      .preview-image {
        width: 100%;
        max-height: 450px;
        border-radius: 8px;
      }
    }
    
    .template-info {
      margin-bottom: 16px;
      
      .title {
        font-size: 18px;
        font-weight: 500;
        color: #333;
        margin-bottom: 8px;
      }
      
      .meta {
        display: flex;
        align-items: center;
        font-size: 14px;
        color: #666;
        
        .divider {
          margin: 0 8px;
          color: #ccc;
        }
      }
    }
    
    .action-buttons {
      display: flex;
      justify-content: space-around;
      background-color: #FFFFFF;
      border-radius: 12px;
      padding: 16px;
      margin-bottom: 20px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
      
      .action-btn {
        display: flex;
        flex-direction: column;
        align-items: center;
        
        text {
          margin-top: 8px;
          font-size: 14px;
          color: #666;
        }
        
        &.complete text {
          color: #5ac725;
        }
      }
    }
    
    .similar-templates {
      margin-bottom: 20px;
      
      .section-title {
        font-size: 16px;
        font-weight: 500;
        color: #333;
        margin-bottom: 12px;
      }
      
      .template-grid {
        display: flex;
        flex-wrap: wrap;
        margin: 0 -6px;
        
        .template-item {
          width: calc(50% - 12px);
          margin: 0 6px 12px;
          background-color: #FFFFFF;
          border-radius: 8px;
          overflow: hidden;
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
          
          image {
            width: 100%;
            height: 120px;
          }
          
          .info {
            padding: 8px;
            
            .name {
              font-size: 14px;
              color: #333;
              margin-bottom: 4px;
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
            }
            
            .year {
              font-size: 12px;
              color: #999;
            }
          }
        }
      }
    }
  }
}
</style> 